{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# 30 Regression Models"
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from lazypredict.Supervised import LazyRegressor\n",
        "from pandas.plotting import scatter_matrix# Scikit-learn packages\n",
        "from sklearn.linear_model import LinearRegression\n",
        "from sklearn.tree import DecisionTreeRegressor\n",
        "from sklearn.ensemble import ExtraTreesRegressor\n",
        "from sklearn import metrics\n",
        "from sklearn.metrics import mean_squared_error# Hide warnings\n",
        "from sklearn.model_selection import train_test_split  \n",
        "\n",
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "\n",
        "import yfinance as yf\n",
        "yf.pdr_override()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "C:\\Users\\Tin Hang\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\deprecation.py:143: FutureWarning: The sklearn.utils.testing module is  deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.utils. Anything that cannot be imported from sklearn.utils is now part of the private API.\n",
            "  warnings.warn(message, FutureWarning)\n"
          ]
        }
      ],
      "execution_count": 1,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "shell.execute_reply": "2021-04-28T00:38:36.736Z",
          "iopub.status.busy": "2021-04-28T00:38:34.815Z",
          "iopub.execute_input": "2021-04-28T00:38:34.824Z",
          "iopub.status.idle": "2021-04-28T00:38:36.720Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# input\n",
        "symbol = 'AMD'\n",
        "start = '2014-01-01'\n",
        "end = '2018-08-27'\n",
        "\n",
        "# Read data \n",
        "dataset = yf.download(symbol,start,end)\n",
        "\n",
        "# Only keep close columns \n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[*********************100%***********************]  1 of 1 completed\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 2,
          "data": {
            "text/plain": "            Open  High  Low  Close  Adj Close    Volume\nDate                                                   \n2014-01-02  3.85  3.98 3.84   3.95       3.95  20548400\n2014-01-03  3.98  4.00 3.88   4.00       4.00  22887200\n2014-01-06  4.01  4.18 3.99   4.13       4.13  42398300\n2014-01-07  4.19  4.25 4.11   4.18       4.18  42932100\n2014-01-08  4.23  4.26 4.14   4.18       4.18  30678700",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n      <th>Adj Close</th>\n      <th>Volume</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2014-01-02</th>\n      <td>3.85</td>\n      <td>3.98</td>\n      <td>3.84</td>\n      <td>3.95</td>\n      <td>3.95</td>\n      <td>20548400</td>\n    </tr>\n    <tr>\n      <th>2014-01-03</th>\n      <td>3.98</td>\n      <td>4.00</td>\n      <td>3.88</td>\n      <td>4.00</td>\n      <td>4.00</td>\n      <td>22887200</td>\n    </tr>\n    <tr>\n      <th>2014-01-06</th>\n      <td>4.01</td>\n      <td>4.18</td>\n      <td>3.99</td>\n      <td>4.13</td>\n      <td>4.13</td>\n      <td>42398300</td>\n    </tr>\n    <tr>\n      <th>2014-01-07</th>\n      <td>4.19</td>\n      <td>4.25</td>\n      <td>4.11</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>42932100</td>\n    </tr>\n    <tr>\n      <th>2014-01-08</th>\n      <td>4.23</td>\n      <td>4.26</td>\n      <td>4.14</td>\n      <td>4.18</td>\n      <td>4.18</td>\n      <td>30678700</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 2,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-04-28T00:38:36.725Z",
          "iopub.execute_input": "2021-04-28T00:38:36.729Z",
          "iopub.status.idle": "2021-04-28T00:38:37.064Z",
          "shell.execute_reply": "2021-04-28T00:38:37.060Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Creating train test split\n",
        "X = dataset.drop(columns=['Adj Close'])\n",
        "y = dataset['Adj Close']\n",
        "\n",
        "offset = int(X.shape[0] * 0.9)\n",
        "\n",
        "X_train, y_train = X[:offset], y[:offset]\n",
        "X_test, y_test = X[offset:], y[offset:]"
      ],
      "outputs": [],
      "execution_count": 3,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-04-28T00:38:37.069Z",
          "iopub.execute_input": "2021-04-28T00:38:37.071Z",
          "iopub.status.idle": "2021-04-28T00:38:37.076Z",
          "shell.execute_reply": "2021-04-28T00:38:37.092Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "reg = LazyRegressor(verbose=0, ignore_warnings=False, custom_metric=None)\n",
        "models, predictions = reg.fit(X_train, X_test, y_train, y_test)\n",
        "print(models)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "100%|██████████| 42/42 [00:02<00:00, 17.49it/s]\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "                               Adjusted R-Squared  R-Squared  RMSE  Time Taken\n",
            "Model                                                                         \n",
            "RANSACRegressor                              1.00       1.00  0.00        0.01\n",
            "Lars                                         1.00       1.00  0.00        0.01\n",
            "HuberRegressor                               1.00       1.00  0.00        0.05\n",
            "LassoLarsCV                                  1.00       1.00  0.00        0.01\n",
            "LassoLarsIC                                  1.00       1.00  0.00        0.01\n",
            "LinearRegression                             1.00       1.00  0.00        0.01\n",
            "TransformedTargetRegressor                   1.00       1.00  0.00        0.01\n",
            "LarsCV                                       1.00       1.00  0.00        0.01\n",
            "OrthogonalMatchingPursuit                    1.00       1.00  0.00        0.01\n",
            "OrthogonalMatchingPursuitCV                  1.00       1.00  0.00        0.01\n",
            "BayesianRidge                                1.00       1.00  0.00        0.01\n",
            "LinearSVR                                    1.00       1.00  0.00        0.02\n",
            "RidgeCV                                      1.00       1.00  0.03        0.01\n",
            "Ridge                                        1.00       1.00  0.10        0.01\n",
            "PassiveAggressiveRegressor                   1.00       1.00  0.11        0.01\n",
            "LassoCV                                      1.00       1.00  0.11        0.05\n",
            "ElasticNetCV                                 1.00       1.00  0.17        0.05\n",
            "SGDRegressor                                 1.00       1.00  0.21        0.01\n",
            "MLPRegressor                                 1.00       1.00  0.21        0.79\n",
            "GeneralizedLinearRegressor                   0.70       0.71  1.87        0.01\n",
            "TweedieRegressor                             0.70       0.71  1.87        0.01\n",
            "ElasticNet                                   0.67       0.68  1.97        0.01\n",
            "Lasso                                        0.63       0.64  2.09        0.01\n",
            "GradientBoostingRegressor                    0.62       0.64  2.11        0.10\n",
            "XGBRegressor                                 0.61       0.63  2.13        0.06\n",
            "ExtraTreesRegressor                          0.59       0.61  2.19        0.15\n",
            "DecisionTreeRegressor                        0.57       0.59  2.23        0.01\n",
            "BaggingRegressor                             0.57       0.59  2.23        0.03\n",
            "RandomForestRegressor                        0.57       0.59  2.24        0.21\n",
            "ExtraTreeRegressor                           0.46       0.49  2.50        0.01\n",
            "KNeighborsRegressor                          0.46       0.48  2.52        0.01\n",
            "LGBMRegressor                                0.45       0.47  2.54        0.05\n",
            "HistGradientBoostingRegressor                0.45       0.47  2.54        0.42\n",
            "AdaBoostRegressor                            0.26       0.29  2.94        0.03\n",
            "NuSVR                                       -0.24      -0.19  3.81        0.06\n",
            "SVR                                         -0.28      -0.22  3.86        0.02\n",
            "GammaRegressor                              -0.35      -0.29  3.97        0.01\n",
            "GaussianProcessRegressor                    -1.86      -1.74  5.78        0.07\n",
            "KernelRidge                                 -2.29      -2.15  6.20        0.04\n",
            "PoissonRegressor                            -3.20      -3.02  7.01        0.01\n",
            "DummyRegressor                              -5.70      -5.42  8.85        0.01\n",
            "LassoLars                                   -5.70      -5.42  8.85        0.01\n"
          ]
        }
      ],
      "execution_count": 4,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2021-04-28T00:38:37.080Z",
          "iopub.execute_input": "2021-04-28T00:38:37.084Z",
          "iopub.status.idle": "2021-04-28T00:38:39.506Z",
          "shell.execute_reply": "2021-04-28T00:38:39.501Z"
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.6.12",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "kernelspec": {
      "argv": [
        "C:/Users/Tin Hang/Anaconda3\\python.exe",
        "-m",
        "ipykernel_launcher",
        "-f",
        "{connection_file}"
      ],
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "nteract": {
      "version": "0.28.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}